﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.Serialization;

namespace ObjectModel.PowerPoint.New
{
    public class LinkedTextRange : LinkedObject
    {
        const string ERR_VALUE = "ERR!";
        //Ppt.TextRange mTextRange;

        public LinkedTextRange() // Parameterless constructor for deserialization.
        {
        }

        public LinkedTextRange(Ppt.TextRange textRange)
        {
            //mTextRange = textRange;
            StoredText = textRange.Text;
            StoredTextRange = new ResolvableSimpleTextRange(textRange);
        }

        public LinkedTextRange(Ppt.TextRange textRange, Ppt.Table table, int row, int col)
        {
            //mTextRange = textRange;
            StoredText = textRange.Text;
            StoredTextRange = new ResolvableTableTextRange(textRange, table, row, col);
        }

        public ResolvableTextRange StoredTextRange { get; set; }

        [XmlIgnore]
        public Ppt.TextRange TextRange
        {
            get
            {
                //if (mTextRange == null)
                //    mTextRange = StoredTextRange.Resolve() as Ppt.TextRange;
                
                //return mTextRange;
                return StoredTextRange.TextRange;
            }

            set
            {
                StoredTextRange.TextRange = value;
            }
        }

        [XmlIgnore]
        public Ppt.TextRange ParentTextRange
        {
            get { return StoredTextRange.Shape.TextFrame.TextRange; }
        }

        [XmlIgnore]
        public ResolvableShape ParentShape
        {
            get { return StoredTextRange.GetParentShape(); }
        }

        public string StoredText { get; set; }

        [XmlIgnore]
        public bool PreviouslySelected { get; set; }

        public void Select()
        {
            TextRange.Select();
            PreviouslySelected = true;
        }

        [XmlIgnore]
        public bool Selected
        {
            get { return StoredTextRange.Selected; }
        }

        [XmlIgnore]
        public bool ContainsSelection
        {
            get { return StoredTextRange.ContainsSelection; }
        }

        public override void UpdateValue()
        {
            string value;            
            try { value = Source.GetValue(); }
            catch { value = ERR_VALUE; }
            TextRange.Text = value;
            StoredText = value;
        }

        public override string ToString()
        {
            return string.Format("{0}: TextRange: Text = \"{1}\"", StoredTextRange.GetParentShape().Name, StoredText);
        }
    }

    //public class LinkedTextRange : LinkedObject
    //{
    //    Ppt.TextRange mTextRange;

    //    public LinkedTextRange() // Parameterless constructor for deserialization.
    //    { }

    //    public LinkedTextRange(Ppt.TextRange textRange)
    //    {
    //        TextRange = textRange;
    //    }

    //    public ResolvableTextRange StoredTextRange
    //    {
    //        get { return StoredTextRange = new ResolvableTextRange(TextRange); }
    //        set { TextRange = value.Resolve() as Ppt.TextRange; }
    //    }

    //    //public SourceObject Source { get; set; }

    //    [XmlIgnore]
    //    public Ppt.TextRange TextRange
    //    {
    //        get { return mTextRange; }

    //        set
    //        {
    //            mTextRange = value;
    //            StoredText = mTextRange.Text;
    //            Shape = new ShapeInfo(mTextRange.GetParent<Ppt.Shape>());
    //        }
    //    }

    //    [XmlIgnore]
    //    public string StoredText { get; private set; }

    //    [XmlIgnore]
    //    public ShapeInfo Shape { get; private set; }

    //    [XmlIgnore]
    //    public bool PreviouslySelected { get; set; }

    //    //public bool Selected
    //    //{
    //    //    get { return TextRange.IsSelected(); }
    //    //}

    //    public void Select()
    //    {
    //        TextRange.Select();
    //        PreviouslySelected = true;
    //    }

    //    public override void UpdateValue()
    //    {
    //        TextRange.Text = Source.GetValue();
    //        StoredText = TextRange.Text;
    //    }

    //    public override string ToString()
    //    {
    //        return string.Format("{0}: TextRange: Text = \"{1}\"", Shape.Name, StoredText);
    //    }
    //}
}
